var mysql = require('mysql');

var MysqlHandler = {
	host:'120.78.193.51',
	user:'root',
	password:'panyi2016',
	database:'androidapp',
	conn:null,
	openConn:function(config){
		var me = this;
		me.conn = mysql.createConnection({
			host:me.host,
			user:me.user,
			password:me.password,
			database:me.database
		});
	},
	closeConn:function(){
        var me = this;
		if(me.conn){
			me.conn.end(function(err){
                if(err){
                    console.log('------------------------connection end false!---------------------------');
                    return;
                }
            });
		}
	},
	exec:function(config){
		var me = this;
		me.openConn();
		me.conn.query(config.sql,config.params,function(err,result){
			if(err)
			{
                console.log("[数据库出错，错误信息为：]");
				console.log(err);
			}
			if(config.callback){
				config.callback(result);
			}
		});
        me.closeConn();
	}
}

module.exports = MysqlHandler;